//
//  HMPickerView.m
//  Ants
//
//  Created by ZHM on 15/8/6.
//  Copyright © 2015年 ZHM. All rights reserved.
//

#import "HMPickerView.h"
#import "HMPickerDataModel.h"

#define screenWidth [UIScreen mainScreen].bounds.size.width
#define screenHeight [UIScreen mainScreen].bounds.size.height
#define keyWindow [[UIApplication sharedApplication] keyWindow]



@interface HMPickerView ()<UIPickerViewDelegate, UIPickerViewDataSource>

@property (nonatomic, weak) UIButton *cancelBtn;
@property (nonatomic, weak) UIButton *sureBtn;
@property (nonatomic, weak) UIPickerView *pickerView;
@property (nonatomic, strong) UIButton *coverView;

/**
 * 存放数据-HMPickerDataModel
 */
@property (strong, nonatomic) NSArray *pickerDataComponent;

@property (strong, nonatomic) NSMutableArray *pickerDataComponent_1;
@property (strong, nonatomic) NSMutableArray *pickerDataComponent_2;

@property (nonatomic, assign) NSInteger index_1;

@end

@implementation HMPickerView
static int const HMPickerViewHeight = 250;

+ (instancetype)pickerViewWithPickerDatas:(NSArray *)arry selectIndex:(NSUInteger)index {
    if (index == -1) {
        ANLog(@"默认");
        index = 0;
    }
    HMPickerView *pickView = [[HMPickerView alloc] init];
    pickView.pickerDataComponent = [HMPickerDataModel pickerDataModelWithStringAry:arry];
    
    [pickView.pickerView reloadAllComponents];
    [pickView.pickerView selectRow:index inComponent:0 animated:NO];
    // 显示
    [pickView appearCouponSheetView];
    return pickView;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self addUpAllChildView];
    }
    return self;
}

- (void)addUpAllChildView{
    // 遮盖
    self.coverView = [UIButton buttonWithType:(UIButtonTypeCustom)];
    _coverView.backgroundColor = [UIColor blackColor];
    _coverView.alpha = 0.0;
    [_coverView addTarget:self action:@selector(disApperCouponSheetView) forControlEvents:UIControlEventTouchUpInside];
    _coverView.frame = CGRectMake(0, 0, screenWidth, screenHeight);
    [keyWindow addSubview:self.coverView];
    
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cancelBtn.frame = CGRectMake(10, 10, 60, 40);
    [cancelBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [cancelBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [self addSubview:cancelBtn];
    _cancelBtn = cancelBtn;
    [cancelBtn addTarget:self action:@selector(disApperCouponSheetView) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    sureBtn.frame = CGRectMake(screenWidth - 60 - 10, 10, 60, 40);
    [sureBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [sureBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [sureBtn setTitle:@"确定" forState:UIControlStateNormal];
    [self addSubview:sureBtn];
    _sureBtn = sureBtn;
    [sureBtn addTarget:self action:@selector(sureButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(cancelBtn.frame), screenWidth, HMPickerViewHeight - cancelBtn.height)];
    pickerView.backgroundColor = [UIColor whiteColor];
    pickerView.delegate = self;
    pickerView.dataSource = self;
    pickerView.showsSelectionIndicator = YES;
    _pickerView = pickerView;
    [self addSubview:pickerView];

    self.frame = CGRectMake(0, screenHeight, screenWidth, HMPickerViewHeight);
    self.backgroundColor = [UIColor whiteColor];
    [keyWindow addSubview:self];
}

#pragma mark - - - 按钮的点击事件
- (void)sureButtonClick:(UIButton *)sender{
    NSString *componentStr_1 = @"";
    NSString *componentStr_2 = @"";
    NSString *componentStr_3 = @"";
    if (self.pickerDataComponent.count) {
        HMPickerDataModel *model = [self.pickerDataComponent objectAtIndex:[self.pickerView selectedRowInComponent:0]];
        componentStr_1 = model.name;
    }

    NSString *location = [NSString stringWithFormat:@"%@ %@ %@", componentStr_1, componentStr_2, componentStr_3];
    ANLog(@"准备返回:%@",componentStr_1);
    HMPickerIndexPath indexPath = {0,_index_1};
    //返回数据
    self.locationMessage(location,indexPath);
    [self disApperCouponSheetView];
}
// 出现
- (void)appearCouponSheetView{
    [UIView animateWithDuration:0.25 animations:^{
        self.transform = CGAffineTransformMakeTranslation(0, - HMPickerViewHeight);
        self.coverView.alpha = 0.4;
    }];
}
// 消失
- (void)disApperCouponSheetView{
    [UIView animateWithDuration:0.25 animations:^{
        self.transform = CGAffineTransformMakeTranslation(0, HMPickerViewHeight);
        self.coverView.alpha = 0.0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        [self.coverView removeFromSuperview];
    }];
}

#pragma mark - UIPicker UIPickerViewDataSource
// 返回列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}
// 每列多少行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return self.pickerDataComponent.count;
}
#pragma mark - UIPicker UIPickerViewDelegate
// 返回当前行的内容, 此处是将数组中数值添加到滚动的那个显示栏上
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    HMPickerDataModel *data = self.pickerDataComponent[row];
    return data.name;
}
// 每列宽度
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    if (component == 0) {
        return 110;
    } else if (component == 1) {
        return 100;
    } else {
        return 110;
    }
}
// 返回选中的行
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if (component == 0) {
        self.pickerDataComponent_1 = self.pickerDataComponent[row];
        _index_1 = row;
    }
}


- (void)layoutSubviews {
    [super layoutSubviews];
}
@end
